package Demo8;

import java.util.Scanner;

//public class Demo8 {
//    private static int flag = 0;
//    private static int count = 0;
//
//    public static void main(String[] args) throws InterruptedException {
//
//        Thread t1 = new Thread(() -> {
//            // 此处搞一个循环
//            while (flag == 0) {
//                count++;
//            }
//            System.out.println("t1 结束");
//        });
//
//        Thread t2 = new Thread(() -> {
//            // 修改 flag 的值
//            Scanner scanner = new Scanner(System.in);
//            System.out.println("请输入一个 flag 的值: ");
//            flag = scanner.nextInt();
//            System.out.println("t2 结束");
//        });
//        t1.start();
//        t2.start();
//        t1.join();
//        t2.join();
//    }
//}


public class Demo8 {
    // 使用 volatile 关键字修饰 flag 变量
    private static volatile int flag = 0;
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            // 此处搞一个循环
            while (flag == 0) {
                count++;
            }
            System.out.println("t1 结束");
        });

        Thread t2 = new Thread(() -> {
            // 修改 flag 的值
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个 flag 的值: ");
            flag = scanner.nextInt();
            System.out.println("t2 结束");
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

    }
}